package tests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import control.Communication;

/**
 * .
 */
public class CommunicationTests {

    /**
     * .
     * 
     * @param name
     *            .
     * @param pw
     *            .
     * @return .
     * @throws NoSuchAlgorithmException .
     */
    public static String login( String name, String pw ) throws NoSuchAlgorithmException
    {
	try
	{
	    String message = "uNick=" + URLEncoder.encode( name, "UTF-8" ) + "&uPw="
		    + URLEncoder.encode( convertMD5( pw ), "UTF-8" );
	    URL url = new URL( "http://coffeeandbits.de/corresChess/backend/login.php" );
	    HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
	    urlConn.setRequestMethod( "POST" );
	    urlConn.setDoInput( true );
	    urlConn.setDoOutput( true );
	    urlConn.setUseCaches( false );
	    urlConn.setRequestProperty( "User-Agent",
		    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2" );
	    // send login query
	    OutputStreamWriter writer = new OutputStreamWriter( urlConn.getOutputStream() );
	    writer.write( message );
	    writer.close();

	    // receive message
	    BufferedReader reader = new BufferedReader( new InputStreamReader( urlConn.getInputStream() ) );

	    for ( String line; ( line = reader.readLine() ) != null; )
	    {

		if ( line.split( "#" )[0].equalsIgnoreCase( "S" ) )
		{
		    return line;
		}
		else if ( line.split( "#" )[0].equalsIgnoreCase( "F" ) )
		{
		    System.out.println( "LOGIN-Fehler" );
		    // proper error handling =D
		}
	    }
	    reader.close();
	}
	catch ( MalformedURLException e )
	{
	    e.printStackTrace();
	}
	catch ( IOException e )
	{
	    e.printStackTrace();
	}
	return null;
    }

    /**
     * .
     * 
     * @param name
     *            .
     * @return .
     * @throws NoSuchAlgorithmException .
     */
    public static String userList( String name ) throws NoSuchAlgorithmException
    {
	try
	{
	    String message = "uNick=" + URLEncoder.encode( name, "UTF-8" );
	    URL url = new URL( "http://coffeeandbits.de/corresChess/backend/userList.php" );
	    HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
	    urlConn.setRequestMethod( "POST" );
	    urlConn.setDoInput( true );
	    urlConn.setDoOutput( true );
	    urlConn.setUseCaches( false );
	    urlConn.setRequestProperty( "User-Agent",
		    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2" );
	    // send login query
	    OutputStreamWriter writer = new OutputStreamWriter( urlConn.getOutputStream() );
	    writer.write( message );

	    // receive message
	    BufferedReader reader = new BufferedReader( new InputStreamReader( urlConn.getInputStream() ) );

	    for ( String line; ( line = reader.readLine() ) != null; )
	    {
		if ( line.split( "#" )[0].equalsIgnoreCase( "S" ) )
		{
		    return line;
		}
		else if ( line.split( "#" )[0].equalsIgnoreCase( "F" ) )
		{
		    System.out.println( "LOGIN-Fehler" );
		    // proper error handling =D
		}
	    }
	    writer.close();
	    reader.close();
	}
	catch ( MalformedURLException e )
	{
	    e.printStackTrace();
	}
	catch ( IOException e )
	{
	    e.printStackTrace();
	}
	return null;
    }

    /**
     * .
     * 
     * @param str
     *            .
     * @return .
     * @throws NoSuchAlgorithmException .
     */
    private static String convertMD5( String str ) throws NoSuchAlgorithmException
    {
	/* Berechnung */
	MessageDigest md5 = MessageDigest.getInstance( "MD5" );
	md5.reset();
	md5.update( str.getBytes() );
	byte[] result = md5.digest();

	/* Ausgabe */
	StringBuffer hexString = new StringBuffer();
	for ( int i = 0; i < result.length; i++ )
	{
	    if ( result[i] <= 15 && result[i] >= 0 )
	    {
		hexString.append( "0" );
	    }
	    hexString.append( Integer.toHexString( 0xFF & result[i] ) );
	}
	return hexString.toString();
    }

    /**
     * Main halt
     * 
     * @param args
     *            .
     */
    public static void main( String[] args )
    {

	Communication.finishMatch( "Jace", 5, 4 );
	// System.out.println( login( "Daves", "test" ) );
	// System.out.println( userList( "Daves" ) );

    }

}
